<?php
declare(strict_types=1);
// 商品档案
namespace app\tjy_v1\controller;
use think\facade\Db; //门面模式
use app\tjy_v1\model\Goods as mysql; //导入模型
class Goods
{
    // 商品档案找到一条
    public function id()
    {
        $query = request()->param(); //请求参数
        $一个商品示例 = [
            "basic" => [
                "field" => [
                    "goods_id" => "333",
                    "goods_name" => "333两件套套装裙连衣裙春季女装新款内搭套头针织衫毛衣中长款打底裙",
                    "goods_short_name" => null,
                    "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/139220/40/17156/149715/5fcd845dEd0b7fa22/d5d02cb4d8e3d134.jpg",
                    "shop_price" => "88.00",
                    "market_price" => "0.00",
                    "detail_mobile" => "[{\"type\":\"text\",\"value\":\"dsa\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\"},{\"type\":\"image\",\"value\":\"https:\\/\\/img14.360buyimg.com\\/n1\\/s800x800_jfs\\/t16705\\/64\\/2563290395\\/214030\\/5cf1a5af\\/5afe3ac5Nea067fec.jpg\"},{\"type\":\"image\",\"value\":\"https:\\/\\/img14.360buyimg.com\\/\\/n1\\/\\/s800x800_jfs\\/\\/t19681\\/\\/169\\/\\/2585249147\\/\\/180448\\/\\/90055be6\\/\\/5afe3aebN351c61a6.jpg\"},{\"type\":\"image\",\"value\":\"https:\\/\\/img14.360buyimg.com\\/\\/n1\\/\\/s800x800_jfs\\/\\/t19549\\/\\/74\\/\\/2613777695\\/\\/164858\\/\\/d5d4697f\\/\\/5afe3aedN5e351240.jpg\"}]",
                    "is_sale" => 1,
                    "activity_price" => "77.00",
                    "sales_volume" => 0,
                    "reference_price" => 0,
                    "integral" => 110,
                    "is_pay_integral" => 0,
                    "is_collect" => 0
                ]
            ],
            "attribute" => ["field" => ["text" => ""], "list" => [], "show" => 0],
            "spec" => ["list" => [], "field" => ["text" => "请选择", "images" => []], "show" => 0],
            "sku" => ["list" => [["goods_image" => "", "shop_price" => "88.00", "stock" => "0", "goods_attr" => "", "goods_sku_key" => ""]]],
            "banner" => ["field" => ["image" => "https://img12.360buyimg.com/img/s750x100_jfs/t1/153061/18/15107/99354/6001c03dE08c6dcab/fac73f98de764bc9.png.webp"], "show" => 0],
            "countdown" => ["field" => ["label" => "疯狂抢购", "start" => 1800], "style" => ["background" => "https://img12.360buyimg.com/img/s750x100_jfs/t1/119650/20/7754/47352/5ed632eaE8b4d8095/1e2456ef7139c95f.png", "indicator" => ["color" => "#f71471", "background" => ""]], "show" => 0],
            "slider" => ["list" => [["type" => "image", "value" => "https://img14.360buyimg.com/n1/s800x800_jfs/t16705/64/2563290395/214030/5cf1a5af/5afe3ac5Nea067fec.jpg"], ["type" => "image", "value" => "https://img14.360buyimg.com//n1//s800x800_jfs//t19681//169//2585249147//180448//90055be6//5afe3aebN351c61a6.jpg"], ["type" => "image", "value" => "https://img14.360buyimg.com//n1//s800x800_jfs//t19549//74//2613777695//164858//d5d4697f//5afe3aedN5e351240.jpg"]]],
            "operation" => ["list" => [["text" => "立即预约", "code" => "reserve", "background" => "#ff6034", "background2" => "#ee0a24"]]]
        ];
        $请求参数 = [
            "goods_id" => "333",
        ];
        $一个商品结构 = [
            // 基本信息
            "basic" => [
                "field" => [
                    "goods_id" => "",
                    "goods_name" => "",
                    "goods_short_name" => "",
                    "goods_image" => "",//商品主图
                    "shop_price" => "",//购买价
                    "market_price" => "",
                    "detail_mobile" => "",//详情图文
                    "is_sale" => 1,//是否上架
                    "activity_price" => "",//活动价格
                    "sales_volume" => 0,//真实销量
                    "reference_price" => 0,//划线价
                    //            "final_price" => $db__goods[0]["shop_price"],//积分外的金额购买
                    "integral" => 110,//积分价
                    "is_pay_integral" => 0,//使用积分购物
                    "is_collect" => 0//立即购买条图标
                ],
            ],
            //产品参数
            "attribute" => [
                "field" => [
                    "text" => "",
                ],
                "list" => [],
                "show" => 0//是否显示模块
            ],
            //产品规格
            "spec" => [
                "list" => [],
                "field" => [
                    "text" => "请选择",
                    "images" => []
                ],
                "show" => 0
            ],
            // 选购弹出层
            "sku" => [
                "list" => [
                    [
                        "goods_image" => "",
                        "shop_price" => "",//购买价
                        "stock" => "",//库存
                        "goods_attr" => "",
                        "goods_sku_key" => "",
                    ]
                ]
            ],
            // 活动横幅
            "banner" => [
                "field" => [
                    "image" => "https://img12.360buyimg.com/img/s750x100_jfs/t1/153061/18/15107/99354/6001c03dE08c6dcab/fac73f98de764bc9.png.webp"
                ],
                "show" => 0
            ],
            // 倒计时抢购
            "countdown" => [
                "field" => [
                    "label" => "疯狂抢购",
                    "start" => 1800
                ],
                "style" => [
                    "background" => "https://img12.360buyimg.com/img/s750x100_jfs/t1/119650/20/7754/47352/5ed632eaE8b4d8095/1e2456ef7139c95f.png",
                    "indicator" => [
                        "color" => "#f71471",
                        "background" => ""
                    ]
                ],
                "show" => 0
            ],
            // 主滚动图
            "slider" => [
                "list" => "",//只要image类型
            ],
            // 立即购买条
            "operation" => [
                "list" => [
                    [
                        "text" => "立即预约",
                        "code" => "reserve",
				"note" => "",//按钮注释
                        "background" => "#ff6034",
                        "background2" => "#ee0a24"
                    ]
                ]
            ]
        ];
        // 写文件
        // file_put_contents('bb/web_api_' . date('Y-m-d_H-i-s', time()) . '.json',"pppp" . "\n", FILE_APPEND);
        // 文件地址为服务的运行目录/public/下
        // 读取JSON商品数据
        $colors = array("good_id_1.json", "good_id_2.json", "good_id_3.json");
        $random_color = array_rand($colors);
        $file_path = 'json/' . $colors[$random_color];
        // $file_path = 'json/good_id_2.json';
        if (file_exists($file_path)) {
            // 文件内容读入
            $goods_id = json_decode(file_get_contents($file_path), true);//将整个文件内容读入到一个字符串中-转数组
        } else {
            return json([
                'code' => 201,
                'time' => time(),
                'msg' => "读取数据文件错误",
                'db' => [],
            ]);
        }
        $apidb = [
            'code' => 200,
            'time' => time(),
            'msg' => "获取一个商品详情",
            'query' => $query,
            'db' => $goods_id,
        ];
        return json($apidb);
        $query = request()->param(); //请求参数
        $sqldb = mysql::where([
            ['id', '=', $query['id']],
            // ['dbid', '=',  $query['dbid']],
        ])->findOrEmpty();
        //设置需要输出的属性
        $sqldb = $sqldb->visible([
            //     'id', //主键
        ]);
        // 设置需要隐藏的输出属性
        $sqldb = $sqldb->hidden([
            //     'id', //主键
        ]);
        // 设置需要附加的输出属性
        $sqldb = $sqldb->append([
            'unit_tab', //附加字段-多包装单位商品包
            'array_prc', //附加字段-价格集
            'off_array', //附加字段-开关集
        ]);
        // 设置数据字段获取器
        // $sqldb = $sqldb->withAttr('name', function($value, $data) {
        // return strtolower($value);
        // });
        if ($sqldb->isEmpty()) {
            // echo '数据集为空';
            $isEmpty = true;
        } else {
            $isEmpty = false;
        }
        $sqldb = $sqldb->toArray(); //以数组方式返回
        if ($isEmpty) {
            $msg = '查询失败-对应商品不存在!';
            $code = 202;
        } else {
            $msg = '查询成功!';
            $code = 200;
            // 找关联多包装单位的商品
            $sql_unit_tab = mysql::where([
                ['unit_goods_dbid', '=', $sqldb['dbid']],
            ])->append([
                        'barcode_data', //附加字段-辅助条码
                        'array_prc', //附加字段-价格集
                        'off_array', //附加字段-开关集
                    ])->order("unit_pack", "desc")
                ->select()
                ->toArray();
            if ($sql_unit_tab) {
                for ($i = 0; $i < count($sql_unit_tab); $i++) {
                    // $li_db[$i]['apitxtx'] = json_decode($li_db[$i]['apitxt'], true); //格式化插入
                    // unset($li_db[$i]); //删除数组中的元素
                    // ksort($li_db[$i]);//根据关联数组的键，对数组进行升序排列
                    // 辅助条码
                    $sql_unit_tab[$i]["barcode_data"] = Db::table('admin__i_goods_barcode')->where('goods_dbid', $sql_unit_tab[$i]['dbid'])->select();
                    //管理开关类-大单位的包
                    $off_array = [
                        // 'off_expiration' =>$sql_unit_tab[$i]["off_expiration"], //开关-管保质期_1是0否
                        // 'off_stock' =>$sql_unit_tab[$i]["off_stock"], //开关-管理库存_1是0否
                        // 'off_deduct' =>$sql_unit_tab[$i]["off_deduct"], //开关-开启提成_1是0否
                        // 'off_discount' =>$sql_unit_tab[$i]["off_discount"], //开关-可以打折_1是0否
                        // 'off_after_sale' =>$sql_unit_tab[$i]["off_after_sale"], //开关-拥有售后_1是0否
                        'off_putaway_web' => $sql_unit_tab[$i]["off_putaway_web"], //开关-上架商城_1是0否
                        // 'off_print_label' =>$sql_unit_tab[$i]["off_print_label"], //开关-打印标签_1是0否
                        // 'off_market_goods' =>$sql_unit_tab[$i]["off_market_goods"], //开关-专柜商品_1是0否
                        // 'off_freebie' =>$sql_unit_tab[$i]["off_freebie"], //开关-可为赠品_1是0否
                        'off_operation' => $sql_unit_tab[$i]["off_operation"], //开关-产生业务_1是0否
                    ];
                    // unset($li_db[$i]); //删除数组中的元素
                    unset($sql_unit_tab[$i]["off_expiration"]); //开关-管保质期_1是0否
                    unset($sql_unit_tab[$i]["off_stock"]); //开关-管理库存_1是0否
                    unset($sql_unit_tab[$i]["off_deduct"]); //开关-开启提成_1是0否
                    unset($sql_unit_tab[$i]["off_discount"]); //开关-可以打折_1是0否
                    unset($sql_unit_tab[$i]["off_after_sale"]); //开关-拥有售后_1是0否
                    unset($sql_unit_tab[$i]["off_print_label"]); //开关-打印标签_1是0否
                    unset($sql_unit_tab[$i]["off_market_goods"]); //开关-专柜商品_1是0否
                    unset($sql_unit_tab[$i]["off_freebie"]); //开关-可为赠品_1是0否
                    foreach ($off_array as $Key => $value) {
                        if ($value == 1) {
                            $off_array[$Key] = true;
                        } else {
                            $off_array[$Key] = false;
                        }
                    }
                    ;
                    $sql_unit_tab[$i]["off_array"] = $off_array;
                }
                $sqldb["unit_tab"] = $sql_unit_tab;
            }
            //管理开关类-基本单位的包
            $off_array = [
                'off_expiration' => $sqldb["off_expiration"], //开关-管保质期_1是0否
                'off_stock' => $sqldb["off_stock"], //开关-管理库存_1是0否
                'off_deduct' => $sqldb["off_deduct"], //开关-开启提成_1是0否
                'off_discount' => $sqldb["off_discount"], //开关-可以打折_1是0否
                'off_after_sale' => $sqldb["off_after_sale"], //开关-拥有售后_1是0否
                'off_putaway_web' => $sqldb["off_putaway_web"], //开关-上架商城_1是0否
                'off_print_label' => $sqldb["off_print_label"], //开关-打印标签_1是0否
                'off_market_goods' => $sqldb["off_market_goods"], //开关-专柜商品_1是0否
                'off_freebie' => $sqldb["off_freebie"], //开关-可为赠品_1是0否
                'off_operation' => $sqldb["off_operation"], //开关-产生业务_1是0否
            ];
            foreach ($off_array as $Key => $value) {
                if ($value == 1) {
                    $off_array[$Key] = true;
                } else {
                    $off_array[$Key] = false;
                }
            }
            ;
            $sqldb["off_array"] = $off_array;
        }
        // unset($tem_data); //销毁
        // array_push()	将一个或多个元素插入数组的末尾（入栈）。
        // array_merge()	把一个或多个数组合并为一个数组。
        // array_merge_recursive()	递归地把一个或多个数组合并为一个数组。
        // $count = count($list);
        $apidb = [
            'code' => $code, //状态码
            'msg' => $msg, //状态信息
            // 'query' => $query, //请求参数
            'db' => $sqldb,
        ];
        return (json($apidb));
    }
 // 商品收藏
 public function collect()
 {
     $query = request()->param(); //请求参数
    //  $query["ty"] = "add收藏增加delete收藏取消";//add收藏增加delete收藏取消
     $apidb = [
         'code' => 200,
         'time' => time(),
         'msg' => "商品收藏",
         'query' => $query,
         'cache' => cache("我是谁"),
     ];
     return json($apidb);
 }
    // 商品档案列表
    public function List()
    {
        $ggb = [
            "basic" => [
                "field" => [
                    "goods_id" => "333",
                    "goods_name" => "333两件套套装裙连衣裙春季女装新款内搭套头针织衫毛衣中长款打底裙",
                    "goods_short_name" => null,
                    "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/139220/40/17156/149715/5fcd845dEd0b7fa22/d5d02cb4d8e3d134.jpg",
                    "shop_price" => "88.00",
                    "market_price" => "0.00",
                    "detail_mobile" => "[{\"type\":\"text\",\"value\":\"dsa\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\\u5546\\u54c1\\u4e3b\\u56fe\"},{\"type\":\"image\",\"value\":\"https:\\/\\/img14.360buyimg.com\\/n1\\/s800x800_jfs\\/t16705\\/64\\/2563290395\\/214030\\/5cf1a5af\\/5afe3ac5Nea067fec.jpg\"},{\"type\":\"image\",\"value\":\"https:\\/\\/img14.360buyimg.com\\/\\/n1\\/\\/s800x800_jfs\\/\\/t19681\\/\\/169\\/\\/2585249147\\/\\/180448\\/\\/90055be6\\/\\/5afe3aebN351c61a6.jpg\"},{\"type\":\"image\",\"value\":\"https:\\/\\/img14.360buyimg.com\\/\\/n1\\/\\/s800x800_jfs\\/\\/t19549\\/\\/74\\/\\/2613777695\\/\\/164858\\/\\/d5d4697f\\/\\/5afe3aedN5e351240.jpg\"}]",
                    "is_sale" => 1,
                    "activity_price" => "77.00",
                    "sales_volume" => 0,
                    "reference_price" => 0,
                    "integral" => 110,
                    "is_pay_integral" => 0,
                    "is_collect" => 0
                ]
            ],
            "attribute" => ["field" => ["text" => ""], "list" => [], "show" => 0],
            "spec" => ["list" => [], "field" => ["text" => "请选择", "images" => []], "show" => 0],
            "sku" => ["list" => [["goods_image" => "", "shop_price" => "88.00", "stock" => "0", "goods_attr" => "", "goods_sku_key" => ""]]],
            "banner" => ["field" => ["image" => "https://img12.360buyimg.com/img/s750x100_jfs/t1/153061/18/15107/99354/6001c03dE08c6dcab/fac73f98de764bc9.png.webp"], "show" => 0],
            "countdown" => ["field" => ["label" => "疯狂抢购", "start" => 1800], "style" => ["background" => "https://img12.360buyimg.com/img/s750x100_jfs/t1/119650/20/7754/47352/5ed632eaE8b4d8095/1e2456ef7139c95f.png", "indicator" => ["color" => "#f71471", "background" => ""]], "show" => 0],
            "slider" => ["list" => [["type" => "image", "value" => "https://img14.360buyimg.com/n1/s800x800_jfs/t16705/64/2563290395/214030/5cf1a5af/5afe3ac5Nea067fec.jpg"], ["type" => "image", "value" => "https://img14.360buyimg.com//n1//s800x800_jfs//t19681//169//2585249147//180448//90055be6//5afe3aebN351c61a6.jpg"], ["type" => "image", "value" => "https://img14.360buyimg.com//n1//s800x800_jfs//t19549//74//2613777695//164858//d5d4697f//5afe3aedN5e351240.jpg"]]],
            "operation" => ["list" => [["text" => "立即预约", "code" => "reserve", "background" => "#ff6034", "background2" => "#ee0a24"]]]
        ];
        $query = request()->param(); //请求参数
        $j = [
            "page" => 1,
            "size" => 10,
            "sort" => "",
            "keyword" => "电磁炉",
        ];
        $apidb = [
            'code' => 200,
            'time' => time(),
            'msg' => "获取商品列表",
            'query' => $query,
            'db' => [
                "tbc_flex_row" => 2,//顶部搜索条样式-值1/2
                "total" => 15,
                "list" => [
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/139220/40/17156/149715/5fcd845dEd0b7fa22/d5d02cb4d8e3d134.jpg",
                        "goods_id" => "153546481082105856",
                        "goods_name" => "两件套套装裙连衣裙春季女装新款内搭套头针织衫毛衣中长款打底裙",
                        "shop_price" => "199.00",
                        "final_price" => "199.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/144284/24/17256/147050/5fcdfddeEfaefd541/4f22bbc129c312f3.jpg",
                        "goods_id" => "153601245093138434",
                        "goods_name" => "小熊（Bear）电火锅分体式电煮锅多功能电热锅宿舍锅4L电炒锅多用途锅DHG-P40F2",
                        "shop_price" => "149.00",
                        "final_price" => "149.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/156168/40/7900/148247/60135a66E25dfddf9/cfa73c385c5ed75a.jpg",
                        "goods_id" => "154338107942731778",
                        "goods_name" => "禧美 国产大虾 净重1.8kg 90-108只/盒 对虾 烧烤 火锅 生鲜 海鲜水产",
                        "shop_price" => "126.00",
                        "final_price" => "126.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/44899/31/4928/223659/5d2a7c8fE46b882ba/8ee4f1a11d6b0f64.jpg",
                        "goods_id" => "155103827952750594",
                        "goods_name" => "短袖t恤男夏季纯白色韩版男士纯色圆领半袖体恤打底衫潮流半截袖夏装修身棉弹力上衣",
                        "shop_price" => "58.00",
                        "final_price" => "58.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t23884/24/36497449/456723/81bd9800/5b236267N095864e2.jpg",
                        "goods_id" => "155832710212702210",
                        "goods_name" => "西子映香 真丝丝巾女桑蚕丝杭州丝绸高档真丝围巾女士印花大方巾职场围脖披肩围巾送礼母亲妈妈款",
                        "shop_price" => "298.00",
                        "final_price" => "298.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/130863/13/16133/123291/5fb204e5E6ffdc61a/f5805cfbf1cdf8fc.jpg",
                        "goods_id" => "156567141409681410",
                        "goods_name" => "裙钗记包芯纱秋冬韩版宽松V领毛衣女大码针织衫女打底衫长袖上衣",
                        "shop_price" => "39.00",
                        "final_price" => "39.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/152489/36/11317/77794/5fe43fa3Eaa0ba8c9/631f32bb27cb0c75.jpg",
                        "goods_id" => "156758231248773122",
                        "goods_name" => "ThinkPadThinkBook 14",
                        "shop_price" => "5199.00",
                        "final_price" => "5199.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/160606/37/11132/439954/604643c0E543e9aff/46473f8294f7072a.png",
                        "goods_id" => "157151837003517954",
                        "goods_name" => "华硕（ASUS）VivoBook15s/15 十代英特尔酷睿 15.6英寸轻薄笔记本电脑",
                        "shop_price" => "4189.00",
                        "final_price" => "4189.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/168288/34/8135/79735/60373b86Eb793b295/596817183ac077e0.jpg",
                        "goods_id" => "157166433979977730",
                        "goods_name" => "联想(Lenovo)YOGA 14s 英特尔Evo平台14英寸全面屏超轻薄笔记本电脑",
                        "shop_price" => "5928.00",
                        "final_price" => "5928.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/108888/4/7920/186317/5e61dbd5E1a440477/dfcf0cc3456126f4.jpg",
                        "goods_id" => "157168206114168834",
                        "goods_name" => "南极人情侣睡衣男士睡衣女冰丝仿真丝绸秋冬季长袖开衫女士家居服套装衬衫式衣裤两件套居家服仙鹤国风潮款",
                        "shop_price" => "119.00",
                        "final_price" => "119.00"
                    ]
                ],
            ],
        ];
        return json($apidb);
        // 请求方式验证
        if (!request()->isPost()) {
            $apidb = ['code' => 201, 'msg' => "请求有误-不是Post方式!",];
            return json($apidb);
        }
        ;
        $limit = request()->param('limit', 10, 'trim'); //每页行数
        $current = request()->param('current', 1, 'trim'); //页码
        if ($query["additional"]["use"] == 1) {
            // additional.use启用额外查询方法-0否-1是。
            $SearchField = $query["additional"]["SearchField"]; //搜索字段
            $SearchOperator = $query["additional"]["SearchOperator"]; //搜索查询表达式
            $SearchaAtistop = $query["additional"]["SearchaAtistop"]; //搜索关键词SearchaAtistop:"[4,28,29,30,31,32,33,34]"
        } else {
            $SearchField = request()->param('SearchField', 'id', 'trim'); //搜索字段
            $SearchOperator = request()->param('SearchOperator', 'LIKE', 'trim'); //搜索查询表达式
            $SearchaAtistop = strval(request()->param('SearchaAtistop', "", 'trim')); //搜索关键词
        }
        $SearchaSort = request()->param('SearchaSort', 'DESC', 'trim'); //搜索排序方式ASE,DESC升降序
        $SearchaSortField = request()->param('SearchaSortField', 'id', 'trim'); //排序字段
        // strval();//转成字符串
        // echo gettype(); //打印变量的类型
        // 查询表达式
        if ($SearchOperator == 'LIKE') {
            $SearchaAtistop = '%' . $SearchaAtistop . '%';
        }
        ;
        if ($SearchField == 'time_new' || $SearchField == 'time_up') {
            if ($SearchOperator == '>' || $SearchOperator == '<') {
                $SearchaAtistop = strtotime(strval($SearchaAtistop));
                if ($SearchField == 'time_new') {
                    $SearchField = 'time_new';
                } else if ($SearchField == 'time_up') {
                    $SearchField = 'time_up';
                }
            }
        }
        $help = [
            "db" => [
                "total" => "数据总行数",
                "per_page" => "每页数量",
                "current_page" => "当前页码",
                "last_page" => "最后一页/总页数",
                "data" => "数据集",
                "SearchaAtistop" => $SearchaAtistop
            ]
        ];
        // $reset = Db::table('admin__i_goods_cls')->where('id', 1)->find();
        // $reset_json = json_decode($reset['ids'], true);
        // $sqldbs = mysql::where([['cls_id', 'in', $reset_json[0]]])->order("i", "desc")->select()->toArray(); //以数组方式返回
        $sqldb = mysql::where([
            [$SearchField, $SearchOperator, $SearchaAtistop],
            // ["cls_id", "IN", [1, 150, 129, 128, 123, 124, 154, 120, 121, 122]],
            ["unit_pack", "=", "B"], //只查小包单位
        ])
            ->order($SearchaSortField, $SearchaSort)
            ->paginate([ //[文档]https://www.kancloud.cn/manual/thinkphp6_0/1037638
                'list_rows' => $limit, // 每页数量
                'page' => $current, // 当前页
                // 'query' => $query,//url额外参数
            ]);
        // echo  udb::getLastSql();
        //设置允许输出字段
        $sqldb = $sqldb->visible([
            // 'id', //主键
            // 'dbid', //数据编号.唯一
            // 'db_state', //数据状态
            // 'time_new', //创建时间
            // 'time_up', //修改时间
            // 'name', //商品名称
            // 'cls_id', //商品分类id
            // 'state', //商品状态
            // 'sup_dbid', //主供货商数据编号
        ]);
        // foreach ($sqldb as $value) {
        //     dump($value->nickname);
        //     dump($value['nickname']);
        //     array_push($sqldb, $rs); //把每个数据集记录插入到数组
        // }
        $sqldb2 = $sqldb->toArray();
        //  当前页起始行
        if ($current > 1) {
            $index2 = $limit * $current - $limit + 1;
        } else {
            $index2 = 1;
        }
        // echo "当前总下标：" . $index2;
        for ($x = 0; $x < count($sqldb); $x++) {
            // $li_db[$x]['apitxtx'] = json_decode($li_db[$x]['apitxt'], true); //格式化插入
            // unset($li_db[$x]); //删除数组中的元素
            // ksort($li_db[$x]);//根据关联数组的键，对数组进行升序排列
            $sqldb2["data"][$x]['index1'] = $x + 1;
            $sqldb2["data"][$x]['index2'] = $index2;
            $index2++;
            $reset = null;
            $reset = Db::table('admin__i_goods_cls')->where('id', $sqldb2["data"][$x]['cls_id'])->find();
            $sqldb2["data"][$x]['cls_name'] = !empty($reset["name"]) ? $reset["name"] : "!断链分类" . $sqldb2["data"][$x]['cls_id'];
            // dump($sqldb2["data"][$x]);
            // 
            // if ($sqldb2["data"][$x]["unit_pack"]!= "B") {
            // dump($sqldb2["data"][$x]["unit_pack"]);
            // 'unit_goods_dbid'=> 'string', //大单位商品数据编号(基本单位等于自己)
            // 'unit_pack'=> 'string',//是否大单位_A是B否
            // mysql::update(['unit_goods_dbid' => $sqldb2["data"][$x]["dbid"], 'unit_pack' => 'B'], ['dbid' => $sqldb2["data"][$x]["dbid"]], ['unit_goods_dbid','unit_pack']);
            // }
            // dump($sqldb2);
            // $sqldb[$x]['idb'] = $x++;
        }
        // dump($sqldb2);
        // dump(count($sqldb));
        // 遍历数据集
        // foreach ($sql_db as $rs) {
        // foreach($age as $x=>$x_value)
        // $rs['event_linkman_card_id']="身份证号码呀---保密了";
        // unset($rs['sys_indexer']);//销毁
        // array_push($tem_data, $rs); //把每个数据集记录插入到数组
        // }
        // unset($tem_data); //销毁
        // array_push()	将一个或多个元素插入数组的末尾（入栈）。
        // array_merge()	把一个或多个数组合并为一个数组。
        // array_merge_recursive()	递归地把一个或多个数组合并为一个数组。
        // $count = count($list);
        $apidb = [
            'api' => ["name" => "商品档案列表", "help" => $help], //API信息
            'code' => 200, //状态码
            'msg' => "查询成功！", //状态信息
            'query' => $query, //请求参数
            // '_header' => request()->header('token'),
            '_cache' => cache('userdb_' . request()->header('token')),
            'data' => $sqldb2,
            'aaaaaaaa' => db::getLastSql(), //输出上次执行的sql语句/也可以使用fetchSql方法直接返回当前的查询SQL而不执行，例如：echo User::fetchSql()->find(1);
        ];
        // unset($sqldb2); //销毁
        return (json($apidb));
    }
    // 首页推荐商品列表
    public function IndexList()
    {
        $db__goods = [
            "goods_name",//商品名称 goods
            "goods_image",//商品图片 goods
            "goods_id",
            "shop_price",//单价
            "final_price"//积分
        ];
        $query = request()->param(); //请求参数
        $j = [
            "page" => 1,
            "size" => 10,
        ];
        $apidb = [
            'code' => 200,
            'time' => time(),
            'msg' => "首页推荐商品列表",
            'query' => $query,
            'db' => [
                "total" => 100,
                "list" => [
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/139220/40/17156/149715/5fcd845dEd0b7fa22/d5d02cb4d8e3d134.jpg",
                        "goods_id" => "153546481082105856",
                        "goods_name" => "两件套套装裙连衣裙春季女装新款内搭套头针织衫毛衣中长款打底裙",
                        "shop_price" => "199.00",
                        "final_price" => "199.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/144284/24/17256/147050/5fcdfddeEfaefd541/4f22bbc129c312f3.jpg",
                        "goods_id" => "153601245093138434",
                        "goods_name" => "小熊（Bear）电火锅分体式电煮锅多功能电热锅宿舍锅4L电炒锅多用途锅DHG-P40F2",
                        "shop_price" => "149.00",
                        "final_price" => "149.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/156168/40/7900/148247/60135a66E25dfddf9/cfa73c385c5ed75a.jpg",
                        "goods_id" => "154338107942731778",
                        "goods_name" => "禧美 国产大虾 净重1.8kg 90-108只/盒 对虾 烧烤 火锅 生鲜 海鲜水产",
                        "shop_price" => "126.00",
                        "final_price" => "126.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/44899/31/4928/223659/5d2a7c8fE46b882ba/8ee4f1a11d6b0f64.jpg",
                        "goods_id" => "155103827952750594",
                        "goods_name" => "短袖t恤男夏季纯白色韩版男士纯色圆领半袖体恤打底衫潮流半截袖夏装修身棉弹力上衣",
                        "shop_price" => "58.00",
                        "final_price" => "58.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t23884/24/36497449/456723/81bd9800/5b236267N095864e2.jpg",
                        "goods_id" => "155832710212702210",
                        "goods_name" => "西子映香 真丝丝巾女桑蚕丝杭州丝绸高档真丝围巾女士印花大方巾职场围脖披肩围巾送礼母亲妈妈款",
                        "shop_price" => "298.00",
                        "final_price" => "298.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/130863/13/16133/123291/5fb204e5E6ffdc61a/f5805cfbf1cdf8fc.jpg",
                        "goods_id" => "156567141409681410",
                        "goods_name" => "裙钗记包芯纱秋冬韩版宽松V领毛衣女大码针织衫女打底衫长袖上衣",
                        "shop_price" => "39.00",
                        "final_price" => "39.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/152489/36/11317/77794/5fe43fa3Eaa0ba8c9/631f32bb27cb0c75.jpg",
                        "goods_id" => "156758231248773122",
                        "goods_name" => "ThinkPadThinkBook 14",
                        "shop_price" => "5199.00",
                        "final_price" => "5199.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/160606/37/11132/439954/604643c0E543e9aff/46473f8294f7072a.png",
                        "goods_id" => "157151837003517954",
                        "goods_name" => "华硕（ASUS）VivoBook15s/15 十代英特尔酷睿 15.6英寸轻薄笔记本电脑",
                        "shop_price" => "4189.00",
                        "final_price" => "4189.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/168288/34/8135/79735/60373b86Eb793b295/596817183ac077e0.jpg",
                        "goods_id" => "157166433979977730",
                        "goods_name" => "联想(Lenovo)YOGA 14s 英特尔Evo平台14英寸全面屏超轻薄笔记本电脑",
                        "shop_price" => "5928.00",
                        "final_price" => "5928.00"
                    ],
                    [
                        "goods_image" => "https://img14.360buyimg.com/n1/s800x800_jfs/t1/108888/4/7920/186317/5e61dbd5E1a440477/dfcf0cc3456126f4.jpg",
                        "goods_id" => "157168206114168834",
                        "goods_name" => "南极人情侣睡衣男士睡衣女冰丝仿真丝绸秋冬季长袖开衫女士家居服套装衬衫式衣裤两件套居家服仙鹤国风潮款",
                        "shop_price" => "119.00",
                        "final_price" => "119.00"
                    ]
                ],
            ],
        ];
        return json($apidb);
        // 请求方式验证
        if (!request()->isPost()) {
            $apidb = ['code' => 201, 'msg' => "请求有误-不是Post方式!",];
            return json($apidb);
        }
        ;
        $limit = request()->param('limit', 10, 'trim'); //每页行数
        $current = request()->param('current', 1, 'trim'); //页码
        if ($query["additional"]["use"] == 1) {
            // additional.use启用额外查询方法-0否-1是。
            $SearchField = $query["additional"]["SearchField"]; //搜索字段
            $SearchOperator = $query["additional"]["SearchOperator"]; //搜索查询表达式
            $SearchaAtistop = $query["additional"]["SearchaAtistop"]; //搜索关键词SearchaAtistop:"[4,28,29,30,31,32,33,34]"
        } else {
            $SearchField = request()->param('SearchField', 'id', 'trim'); //搜索字段
            $SearchOperator = request()->param('SearchOperator', 'LIKE', 'trim'); //搜索查询表达式
            $SearchaAtistop = strval(request()->param('SearchaAtistop', "", 'trim')); //搜索关键词
        }
        $SearchaSort = request()->param('SearchaSort', 'DESC', 'trim'); //搜索排序方式ASE,DESC升降序
        $SearchaSortField = request()->param('SearchaSortField', 'id', 'trim'); //排序字段
        // strval();//转成字符串
        // echo gettype(); //打印变量的类型
        // 查询表达式
        if ($SearchOperator == 'LIKE') {
            $SearchaAtistop = '%' . $SearchaAtistop . '%';
        }
        ;
        if ($SearchField == 'time_new' || $SearchField == 'time_up') {
            if ($SearchOperator == '>' || $SearchOperator == '<') {
                $SearchaAtistop = strtotime(strval($SearchaAtistop));
                if ($SearchField == 'time_new') {
                    $SearchField = 'time_new';
                } else if ($SearchField == 'time_up') {
                    $SearchField = 'time_up';
                }
            }
        }
        $help = [
            "db" => [
                "total" => "数据总行数",
                "per_page" => "每页数量",
                "current_page" => "当前页码",
                "last_page" => "最后一页/总页数",
                "data" => "数据集",
                "SearchaAtistop" => $SearchaAtistop
            ]
        ];
        // $reset = Db::table('admin__i_goods_cls')->where('id', 1)->find();
        // $reset_json = json_decode($reset['ids'], true);
        // $sqldbs = mysql::where([['cls_id', 'in', $reset_json[0]]])->order("i", "desc")->select()->toArray(); //以数组方式返回
        $sqldb = mysql::where([
            [$SearchField, $SearchOperator, $SearchaAtistop],
            // ["cls_id", "IN", [1, 150, 129, 128, 123, 124, 154, 120, 121, 122]],
            ["unit_pack", "=", "B"], //只查小包单位
        ])
            ->order($SearchaSortField, $SearchaSort)
            ->paginate([ //[文档]https://www.kancloud.cn/manual/thinkphp6_0/1037638
                'list_rows' => $limit, // 每页数量
                'page' => $current, // 当前页
                // 'query' => $query,//url额外参数
            ]);
        // echo  udb::getLastSql();
        //设置允许输出字段
        $sqldb = $sqldb->visible([
            // 'id', //主键
            // 'dbid', //数据编号.唯一
            // 'db_state', //数据状态
            // 'time_new', //创建时间
            // 'time_up', //修改时间
            // 'name', //商品名称
            // 'cls_id', //商品分类id
            // 'state', //商品状态
            // 'sup_dbid', //主供货商数据编号
        ]);
        // foreach ($sqldb as $value) {
        //     dump($value->nickname);
        //     dump($value['nickname']);
        //     array_push($sqldb, $rs); //把每个数据集记录插入到数组
        // }
        $sqldb2 = $sqldb->toArray();
        //  当前页起始行
        if ($current > 1) {
            $index2 = $limit * $current - $limit + 1;
        } else {
            $index2 = 1;
        }
        // echo "当前总下标：" . $index2;
        for ($x = 0; $x < count($sqldb); $x++) {
            // $li_db[$x]['apitxtx'] = json_decode($li_db[$x]['apitxt'], true); //格式化插入
            // unset($li_db[$x]); //删除数组中的元素
            // ksort($li_db[$x]);//根据关联数组的键，对数组进行升序排列
            $sqldb2["data"][$x]['index1'] = $x + 1;
            $sqldb2["data"][$x]['index2'] = $index2;
            $index2++;
            $reset = null;
            $reset = Db::table('admin__i_goods_cls')->where('id', $sqldb2["data"][$x]['cls_id'])->find();
            $sqldb2["data"][$x]['cls_name'] = !empty($reset["name"]) ? $reset["name"] : "!断链分类" . $sqldb2["data"][$x]['cls_id'];
            // dump($sqldb2["data"][$x]);
            // 
            // if ($sqldb2["data"][$x]["unit_pack"]!= "B") {
            // dump($sqldb2["data"][$x]["unit_pack"]);
            // 'unit_goods_dbid'=> 'string', //大单位商品数据编号(基本单位等于自己)
            // 'unit_pack'=> 'string',//是否大单位_A是B否
            // mysql::update(['unit_goods_dbid' => $sqldb2["data"][$x]["dbid"], 'unit_pack' => 'B'], ['dbid' => $sqldb2["data"][$x]["dbid"]], ['unit_goods_dbid','unit_pack']);
            // }
            // dump($sqldb2);
            // $sqldb[$x]['idb'] = $x++;
        }
        // dump($sqldb2);
        // dump(count($sqldb));
        // 遍历数据集
        // foreach ($sql_db as $rs) {
        // foreach($age as $x=>$x_value)
        // $rs['event_linkman_card_id']="身份证号码呀---保密了";
        // unset($rs['sys_indexer']);//销毁
        // array_push($tem_data, $rs); //把每个数据集记录插入到数组
        // }
        // unset($tem_data); //销毁
        // array_push()	将一个或多个元素插入数组的末尾（入栈）。
        // array_merge()	把一个或多个数组合并为一个数组。
        // array_merge_recursive()	递归地把一个或多个数组合并为一个数组。
        // $count = count($list);
        $apidb = [
            'api' => ["name" => "商品档案列表", "help" => $help], //API信息
            'code' => 200, //状态码
            'msg' => "查询成功！", //状态信息
            'query' => $query, //请求参数
            // '_header' => request()->header('token'),
            '_cache' => cache('userdb_' . request()->header('token')),
            'data' => $sqldb2,
            'aaaaaaaa' => db::getLastSql(), //输出上次执行的sql语句/也可以使用fetchSql方法直接返回当前的查询SQL而不执行，例如：echo User::fetchSql()->find(1);
        ];
        // unset($sqldb2); //销毁
        return (json($apidb));
    }
}
